﻿using System.Linq;
using static DbUtility.EF;

namespace Mud.Characters
{
    public class CharacterDal : ICharacterDal
    {
        public bool ValidatePassword(string name, string password)
        {
            CharacterContext db = new CharacterContext();
            using (db)
            {
                return db.Characters.First(c => c.Name == name).Password == password;
            }
        }

        public CharacterDataModel GetCharacter(string name)
        {
            CharacterContext db = new CharacterContext();
            using (db)
            {
                if (!TableExists(db, "Characters")) return null;
                return db.Characters.FirstOrDefault(character => character.Name == name);
            }
        }

        public bool CharacterExists(string name)
        {
            CharacterContext db = new CharacterContext();
            using (db)
            {
                if (!TableExists(db, "Characters")) return false;
                return db.Characters.Any(character => character.Name == name);
            }
        }
    }
}
